package com.aek.aekdroid;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.aek.aekdroid.data.IDbConstants;

public class DeleteActivity extends BaseActivity {
	public static final String TAG = "DeleteActivity";
	public static final String TYPE = "type";
	public static final int MATCH = 0;
	public static final int DETAIL = 1;
	public static final String MATCH_ID = "MATCH_ID";
	private Button ok;
	private Button cancel;
	private TextView title;
	private TextView confirm;
	private int type;
	private int matchId = -1;
	private int detailId = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.delete);
		title = (TextView) findViewById(R.id.delete_title);
		confirm = (TextView) findViewById(R.id.delete_confirmation);
		ok = (Button) findViewById(R.id.delete_ok);
		cancel = (Button) findViewById(R.id.delete_cancel);
		cancel.setOnClickListener(new ClickListener());
		ok.setOnClickListener(new ClickListener());
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			type = bundle.getInt(TYPE);
			matchId = bundle.getInt(MATCH_ID, -1);
		}
		switch (type) {
		case MATCH:
			title.setText(R.string.delete_deleteMatch);
			confirm.setText(R.string.delete_confirmDeleteMatch);
			break;
		case DETAIL:
			title.setText(R.string.delete_deleteDetail);
			confirm.setText(R.string.delete_confirmDeleteDetail);
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}

	private void submit() {
		String pass = aek.getPassword();
		String url = "";
		switch (type) {
		case MATCH:
			url = WebAekDroid.DELETE_MATCH + "&pass=" + pass + "&id=" + this.matchId;
			MyLog.d(TAG, "Deleting match " + url);
			break;
		case DETAIL:
			url = WebAekDroid.DELETE_DETAIL + "&pass=" + pass + "&id=" + this.detailId;
			MyLog.d(TAG, "Deleting detail " + url);
			break;
		default:
			break;
		}
		Helper.adminFunction(this, url);

	}

	@Override
	public void onBackPressed() {
		switch (type) {
		case MATCH:
			Intent scheduleActivity = new Intent(this, ScheduleActivity.class);
			startActivity(scheduleActivity);
		case DETAIL:
			Intent showMatchIntent = new Intent(this, ShowMatchActivity.class);
			showMatchIntent.putExtra(IDbConstants.MATCHES_C_ID, matchId);
			startActivity(showMatchIntent);
		default:
			break;
		}

	}

	class ClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			if (v.getId() == R.id.delete_ok) {
				DeleteActivity.this.submit();
			} else if (v.getId() == R.id.delete_cancel) {
				finish();
			}

		}

	}
}
